<?php

class Silk_Zgb_Adminhtml_ReportController extends Mage_Adminhtml_Controller_Action
{	
	public function regionAction()
	{
		$this->loadLayout();
		$this->renderLayout();
	}

	public function productAction()
	{
		$this->loadLayout();
		$this->renderLayout();
	}

	public function stockAction()
	{
		$this->loadLayout();
		$this->renderLayout();
	}

	public function customerAction()
	{
		$this->loadLayout();
		$this->renderLayout();
	}

	public function salesAction()
	{
		$export = $this->getRequest()->getParam('export');
		if(!$export)
		{
			$this->loadLayout();
			$this->renderLayout();
		}
		else
		{
			$block = new Silk_Zgb_Block_Report_Sales();
			$objWriter = $block->exportToExcel();
			header("Pragma: public");
	        header("Expires: 0");
	        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
	        //header("Content-Type:application/force-download");
	        //header("Content-Type:application/vnd.ms-execl");
	        header("Content-Type:application/octet-stream;charset=utf-8");
	        //header("Content-Type:application/download");
	        if(strpos($_SERVER['HTTP_USER_AGENT'],"MSIE"))
	        {
				header('Content-Disposition:attachment;filename="'.urlencode("销售统计报表.xlsx").'"');
	        }
	        else
	        {
	        	header('Content-Disposition:attachment;filename="销售统计报表.xlsx"');
	        }
	        
	        header("Content-Transfer-Encoding:binary");
	        $objWriter->save('php://output');
			// $this->getResponse()->setHeader("Pragma","public");
	  //       $this->getResponse()->setHeader("Expires","0");
	  //       $this->getResponse()->setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
	  //       $this->getResponse()->setHeader("Content-Type","application/force-download");
	  //       $this->getResponse()->setHeader("Content-Type","application/vnd.ms-execl");
	  //       $this->getResponse()->setHeader("Content-Type","application/octet-stream");
	  //       $this->getResponse()->setHeader("Content-Type","application/download");;
	  //       $this->getResponse()->setHeader('Content-Disposition","attachment;filename="分类数据.xlsx"');
	  //       $this->getResponse()->setHeader("Content-Transfer-Encoding","binary");
	  //       $this->getResponse()
   //          	->setHttpResponseCode(200)
	  //           ->setHeader('Pragma', 'public', true)
	  //           ->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true)
	  //           ->setHeader('Content-type', 'application/octet-stream', true)
	  //           ->setHeader('Content-Length', is_null($contentLength) ? strlen($content) : $contentLength, true)
	  //           ->setHeader('Content-Disposition', 'attachment; filename="销售统计报表.xlsx"', true)
	  //           ->setHeader('Last-Modified', date('r'), true);
	        //$objWriter->save('php://output');
		}
		
	}

	public function orderAction()
	{
		$export = $this->getRequest()->getParam('export');
		
		if(!$export)
		{
			$this->loadLayout();
			$this->renderLayout();
		}
		else
		{
			$block = new Silk_Zgb_Block_Report_Order();
			$objWriter = $block->exportToExcel();
			header("Pragma: public");
	        header("Expires: 0");
	        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
	        //header("Content-Type:application/force-download");
	        //header("Content-Type:application/vnd.ms-execl");
	        header("Content-Type:application/octet-stream;charset=utf-8");
	        //header("Content-Type:application/download");
	        if(strpos($_SERVER['HTTP_USER_AGENT'],"MSIE"))
	        {
	        	header('Content-Disposition:attachment;filename="'.urlencode("订单统计报表.xlsx").'"');
	        }
	        else
	        {
	        	header('Content-Disposition:attachment;filename="订单统计报表.xlsx"');
	        }
	        
	        header("Content-Transfer-Encoding:binary");
	        $objWriter->save('php://output');
	    }
	}

	public function itemAction()
	{
		$export = $this->getRequest()->getParam('export');
		
		if(!$export)
		{
			$this->loadLayout();
			$this->renderLayout();
		}
		else
		{
			$block = new Silk_Zgb_Block_Report_Item();
			$objWriter = $block->exportToExcel();
			header("Pragma: public");
	        header("Expires: 0");
	        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
	        //header("Content-Type:application/force-download");
	        //header("Content-Type:application/vnd.ms-execl");
	        header("Content-Type:application/octet-stream;charset=utf-8");
	        //header("Content-Type:application/download");
	        if(strpos($_SERVER['HTTP_USER_AGENT'],"MSIE"))
	        {
	        	header('Content-Disposition:attachment;filename="'.urlencode("订单明细统计报表.xlsx").'"');
	        }
	        else
	        {
	        	header('Content-Disposition:attachment;filename="订单明细统计报表.xlsx"');
	        }
	        
	        header("Content-Transfer-Encoding:binary");
	        $objWriter->save('php://output');
	    }
	}

	public function shopAction()
	{
		$export = $this->getRequest()->getParam('export');
		
		if(!$export)
		{
			$this->loadLayout();
			$this->renderLayout();
		}
		else
		{
			$block = new Silk_Zgb_Block_Report_Shop();
			$objWriter = $block->exportToExcel();
			header("Pragma: public");
	        header("Expires: 0");
	        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
	        //header("Content-Type:application/force-download");
	        //header("Content-Type:application/vnd.ms-execl");
	        header("Content-Type:application/octet-stream;charset=utf-8");
	        //header("Content-Type:application/download");;
	        if(strpos($_SERVER['HTTP_USER_AGENT'],"MSIE"))
	        {
	        	header('Content-Disposition:attachment;filename="'.urlencode("终端用户统计报表.xlsx").'"');
	        }
	        else
	        {
	        	header('Content-Disposition:attachment;filename="终端用户统计报表.xlsx"');
	        }
	        
	        header("Content-Transfer-Encoding:binary");
	        $objWriter->save('php://output');
	    }
	}

	public function listAction()
	{
		$export = $this->getRequest()->getParam('export');
		
		if(!$export)
		{
			$this->loadLayout();
			$this->renderLayout();
		}
		else
		{
			$block = new Silk_Zgb_Block_Report_List();
			$objWriter = $block->exportToExcel();
			header("Pragma: public");
	        header("Expires: 0");
	        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
	        //header("Content-Type:application/force-download");
	        //header("Content-Type:application/vnd.ms-execl");
	        header("Content-Type:application/octet-stream;charset=utf-8");
	        //header("Content-Type:application/download");;
	        if(strpos($_SERVER['HTTP_USER_AGENT'],"MSIE"))
	        {
	        	header('Content-Disposition:attachment;filename="'.urlencode("商品清单.xlsx").'"');
	        }
	        else
	        {
	        	header('Content-Disposition:attachment;filename="商品清单.xlsx"');
	        }
	        
	        header("Content-Transfer-Encoding:binary");
	        $objWriter->save('php://output');
		}
	}

	public function ordersumAction()
	{
		$this->loadLayout();
		$this->renderLayout();
	}


	protected function _isAllowed()
    {
        return Mage::getSingleton('admin/session')->isAllowed('zgb_report');
    }
}
